package com.dingreading.cloud.app.util;

import com.dingreading.cloud.app.entity.Corporations;
import com.dingreading.cloud.app.entity.Members;
import com.dingreading.cloud.app.entity.Staff;
import com.dingreading.cloud.app.entity.Stores;
import com.dingreading.cloud.app.service.MembersService;
import com.dingreading.cloud.app.service.StaffService;
import com.dingreading.cloud.app.service.StoresService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class UidUtil {

    public static UidUtil util;

    @PostConstruct
    public void init() {
        util = this;
    }

    @Autowired
    private StoresService storesService;
    @Autowired
    private MembersService membersService;
    @Autowired
    private StaffService staffService;

    /**
     * 根据门店uid获取
     *
     * @param storeUid 门店uid
     */
    public static Object getStore(String storeUid) {
        if (StringUtils.isBlank(storeUid)) return "门店uid不能为空";

        Stores storesObj = util.storesService.getByUid(storeUid);
        if (storesObj == null) return "门店不存在";

        if (!storesObj.getEnabled().equals(1)) return "门店已被禁用";

        return storesObj;
    }

    /**
     * 根据会员uid获取
     *
     * @param memberUid 会员uid
     */
    public static Object getMember(String memberUid) {
        if (StringUtils.isBlank(memberUid)) return "会员uid不能为空";

        Members membersObj = util.membersService.getByUid(null, memberUid);
        if (membersObj == null) return "会员不存在";

        if (!membersObj.getEnabled().equals(1)) return "会员已被禁用";

        return membersObj;
    }

    /**
     * 根据门店员工uid获取
     *
     * @param staffUid 门店员工uid
     */
    public static Object getStaff(String staffUid) {
        if (StringUtils.isBlank(staffUid)) return "门店员工uid不能为空";

        Staff staffObj = util.staffService.getByUid(staffUid);
        if (staffObj == null) return "门店员工不存在";

        if (!staffObj.getEnabled().equals(1)) return "门店员工已被禁用";

        return staffObj;
    }

    /**
     * 验证公司信息
     */
    public static String judgeCorp(Corporations corporations) {
        if (corporations == null)
            return "未查询到公司信息";

        if (!corporations.getEnabled().equals(1))
            return "账户已停用";

        return "";
    }
}